﻿var dtCh = "/";

function PopUpShowingCenter(sender, eventArgs) 
{
    var popUp;
    popUp = eventArgs.get_popUp();
    var gridWidth = sender.get_element().offsetWidth;
    var gridHeight = sender.get_element().offsetHeight;
    var popUpWidth = popUp.style.width.substr(0, popUp.style.width.indexOf("px"));
    var popUpHeight = popUp.style.height.substr(0, popUp.style.height.indexOf("px"));
    popUp.style.left = ((gridWidth - popUpWidth) / 2 + sender.get_element().offsetLeft).toString() + "px";
    popUp.style.top = ((gridHeight - popUpHeight) / 2 + sender.get_element().offsetTop).toString() + "px";
}

function openRadWin(formname, popname) 
{
    var oWnd = radopen(formname, popname);
}

function onRowDropping(sender, args) 
{
    if (currentNode) 
    {
        $get("nodeValueHiddenField").value = currentNode.get_value();
        args.set_destinationHtmlElement("nodeValueHiddenField");
    }
    else 
    {
        args.set_cancel(true);
    }
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    year = year - 543;
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}

function validateDate(src, args) {
    if (args.Value.length > 0) {
        if (isDate(args.Value) == false) {
            args.IsValid = false;
        }
    }
}

function isDate(dtStr) {
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strDay = dtStr.substring(0, pos1)
    var strMonth = dtStr.substring(pos1 + 1, pos2)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear
    if (strDay.charAt(1) == "0" && strDay.length > 1) strDay = strDay.substring(2)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)
    if (pos1 == -1 || pos2 == -1) {
        return false
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        return false
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        return false
    }
    if (strYear.length != 4 || year == 0) {
        return false
    }
    return true
}

function changeMonthNumberToName(month_number) {
    var months = new Array(12);
    months[1] = "มกราคม";
    months[2] = "กุมภาพันธ์";
    months[3] = "มีนาคม";
    months[4] = "เมษายน";
    months[5] = "พฤษภาคม";
    months[6] = "มิถุนายน";
    months[7] = "กรกฎาคม";
    months[8] = "สิงหาคม";
    months[9] = "กันยายน";
    months[10] = "ตุลาคม";
    months[11] = "พฤศจิกายน";
    months[12] = "ธันวาคม";
    return months[month_number];
}

function MultipleFieldsValidatorEvaluateIsValid(val) {
    controltovalidateIDs = val.controlstovalidate.split(',');
    switch (val.condition) {
        case 'OR':
            for(var controltovalidateIDIndex in controltovalidateIDs) {
                var controlID = controltovalidateIDs[controltovalidateIDIndex];
                if (ValidatorTrim(ValidatorGetValue(controlID)) != '') {
                    return true;
                } 
            }
            return false;
        break;
        case 'XOR':
            for(var controltovalidateIDIndex in controltovalidateIDs) {
                var controlID = controltovalidateIDs[controltovalidateIDIndex];
                if (controltovalidateIDIndex == '0') {
                    var previousResult = !(ValidatorTrim(ValidatorGetValue(controlID)) == '');
                    continue;
                }
                var currentResult = !(ValidatorTrim(ValidatorGetValue(controlID)) == '');
                if (currentResult != previousResult) {
                    return true;
                }
                previousResult != currentResult;
            }
            return false;
        break;
        case 'AND':
            for(var controltovalidateIDIndex in controltovalidateIDs) {
                var controlID = controltovalidateIDs[controltovalidateIDIndex];
                if (ValidatorTrim(ValidatorGetValue(controlID)) == '') {
                    return false;
                } 
            }
            return true;
        break;
    }
    return false;
}
